package com.imooc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.exceptions.GraceException;
import com.imooc.grace.result.ResponseStatusEnum;
import com.imooc.mapper.AdminMapper;
import com.imooc.pojo.Admin;
import com.imooc.pojo.bo.AdminBO;
import com.imooc.pojo.bo.CreateAdminBO;
import com.imooc.pojo.bo.UpdateAdminBO;
import com.imooc.service.AdminService;
import com.imooc.utils.MD5Utils;
import com.imooc.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 慕聘网运营管理系统的admin账户表，仅登录，不提供注册 服务实现类
 * </p>
 *
 * @author 椋庨棿褰辨湀
 * @since 2025-09-10
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

	@Autowired
	private AdminMapper adminMapper;


	@Override
	public void deleteAdmin(String username) {
		adminMapper.delete(new QueryWrapper<Admin>().eq("username", username));
	}

	@Override
	public Admin getById(String adminId) {
		return adminMapper.selectById(Integer.parseInt(adminId));
	}

	@Override
	public void updateAdmin(UpdateAdminBO adminBO) {
		Admin admin = new Admin();
		BeanUtils.copyProperties(adminBO, admin);
		admin.setUpdatedTime(LocalDateTime.now());
		adminMapper.updateById(admin);
	}

	@Transactional
	@Override
	public void createAdmin(CreateAdminBO createAdminBO) {
		Admin selfAdmin = getSelfAdmin(createAdminBO.getUsername()); //查询用户名是否存在
		if(selfAdmin !=null ){
			GraceException.display(ResponseStatusEnum.ADMIN_USERNAME_EXIST_ERROR);
		}
		// 生成随机数字或者英文字母结合的盐
		String slat = (int) ((Math.random() * 9 + 1) * 100000) + "";
		Admin newAdmin = new Admin();
		newAdmin.setSlat(slat);
		BeanUtils.copyProperties(createAdminBO, newAdmin);
		String pwd = MD5Utils.encrypt(createAdminBO.getPassword(), slat); //将加盐的密码保存
		newAdmin.setPassword(pwd);

		newAdmin.setCreateTime(LocalDateTime.now());
		newAdmin.setUpdatedTime(LocalDateTime.now());

		adminMapper.insert(newAdmin);
	}

	@Override
	public PagedGridResult getAdminList(String accountName, Integer page, Integer limit) {
		QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
//		两种方式可以实现分页
		PageHelper.startPage(page, limit);
		if (StringUtils.isNoneBlank(accountName)) {
			adminQueryWrapper.eq("username", accountName);
		}
 		List<Admin> admins = adminMapper.selectList(adminQueryWrapper);
		PagedGridResult pagedGridResult = setterPagedGrid(admins,page);

//		第二个 todo bug 分页个数无效
//		Page<Admin> pageAdminList = new Page(page,limit);
//		Page<Admin> adminPage = adminMapper.selectPage(pageAdminList, adminQueryWrapper);
		return pagedGridResult;
//		return setterPagedGrid(adminPage.getRecords(), page);
	}

	public PagedGridResult setterPagedGrid(List<?> list,
	                                       Integer page) {
		PageInfo<?> pageList = new PageInfo<>(list);
		PagedGridResult gridResult = new PagedGridResult();
		gridResult.setRows(list);
		gridResult.setPage(page);
		gridResult.setRecords(pageList.getTotal());
		gridResult.setTotal(pageList.getPages());
		return gridResult;
	}

	@Override
	public boolean adminLogin(AdminBO adminBO) {
		Admin selfAdmin = getSelfAdmin(adminBO.getUsername());
		if(selfAdmin == null ){
			return false;
		}
		String slat = selfAdmin.getSlat(); //获取密码的盐
		String md5Str = MD5Utils.encrypt(adminBO.getPassword(), slat);
		if(selfAdmin.getPassword().equalsIgnoreCase(md5Str)){
			return true;
		}
		return false;
	}

	@Override
	public Admin getAdminInfo(AdminBO adminBO) {
		return getSelfAdmin(adminBO.getUsername());
	}

	private Admin getSelfAdmin(String username) {
		Admin admin = adminMapper.selectOne(
				new QueryWrapper<Admin>()
						.eq("username", username)
		);
		return admin;
	}



}
